<?php
namespace app\crm\controller;
use app\common\controller\UserBase; 
use app\crm\model\CrmCustomer;
use app\crm\model\CrmTrack;
use app\user\model\FieldData;
use app\user\model\User;

class Track extends UserBase
{
    public function initialize()
    {
        parent::initialize();
        //获取客服列表
        $this->contact = get_dict_list('6');
        //获取字段列表
        $this->field = FieldData::get_field_list('2');
    }
    public function index_json($customer_id='',$limit='')
    {
        $track = new CrmTrack();
        if($customer_id <> ''){
            $track = $track->where('customer_id', $customer_id);
        }
        $list = $track->with(['user'])->order('id desc')->paginate($limit);
        $this->result($list);
    }
    //添加客户
    public function add($customer_id)
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $param['jdata'] = $param;
            $param['user_id'] = UserId();
            $param['shop_id'] = ShopId();
            $param['customer_id'] = $customer_id;
            $result = CrmTrack::create($param);
            if ($result == true) {
                if(isset($param['is_next']) and $param['is_next'] == 'on'){
                    insert_notice($param['datetime'], '1', ['customer_id'=>$customer_id,'id'=>$result->id,'show'=>'track'], '客户回访提醒');
                }
                insert_user_log('添加了客户回访');
                $this->success('添加成功',url('crm/detail/index',['customer_id'=>$customer_id,'show'=>'track']));
            } else {
                $this->error($this->errorMsg);
            }
        }
        
        return $this->fetch('save', [
            'field'     => $this->field,
        ]);
    }
    //编辑联系人
    public function edit($id)
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $result = CrmTrack::update($param,['id'=>$param['id']]);
            if ($result == true) {
                insert_user_log('修改了客户回访');
                $this->success('修改成功');
            } else {
                $this->error($this->errorMsg);
            }
        }
        $data = CrmTrack::where('id', $id)->find();
        return $this->fetch('save', [
            'data'      => $data,
            'field'     => $this->field,
        ]);
    }
    //删除客户
    public function del()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            CrmTrack::destroy($param['id']);
            insert_user_log('删除了客户回访');
            $this->success('删除成功');
        }
    }

    
    
    
}
